//
//  NSObject+JSON_Additions.m
//  OrderSeat
//
//  Created by 王威 on 13-10-26.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "JSON+Additions.h"

@implementation NSData (JSON)

- (NSDictionary*)JSONValue
{
    NSError *error = nil;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:self options:NSJSONReadingMutableContainers error:&error];
    if (error) {
        CLog(@"JSON Format Error! %@", error);
        
        NSString *result = [[NSString alloc] initWithData:self encoding:NSUTF8StringEncoding];
        NSString *jsonStr = [result stringByReplacingOccurrencesOfString:@"\r" withString:@""];
        jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"\t" withString:@""];
        
        return [jsonStr JSONValue];
    }
    
    return json;
}

- (NSDictionary*)JSONValueWithGBK
{
    NSError *error = nil;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:self options:NSJSONReadingMutableContainers error:&error];
    if (error) {
        CLog(@"JSON Format Error! %@", error);
        
        NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
        
        NSString *result = [[NSString alloc] initWithData:self encoding:enc];
        NSString *jsonStr = [result stringByReplacingOccurrencesOfString:@"\r" withString:@""];
        jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"\t" withString:@""];
        
        return [jsonStr JSONValue];
    }
    
    return json;
}

@end

@implementation NSString (JSON)

- (NSDictionary*)JSONValue
{
    NSError *error = nil;
    
    NSString *jsonStr = [self stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"\t" withString:@""];
    
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
    
    if (error) {
        CLog(@"JSON Format Error! %@", error);
    }
    
    return json;
}

@end

@implementation NSArray (JSON)

- (NSString*)JSONString
{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    
    if (error) {
        CLog(@"JSON Format Error! %@", error);
    }
    
    NSString *jsonString = [[NSString alloc] initWithData:jsonData
                                                 encoding:NSUTF8StringEncoding];
    return jsonString;
}

@end

@implementation NSDictionary (JSON)

- (NSString*)JSONString
{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    
    if (error) {
        CLog(@"JSON Format Error! %@", error);
    }
    
    NSString *jsonString = [[NSString alloc] initWithData:jsonData
                                                 encoding:NSUTF8StringEncoding];
    return jsonString;
}

@end
